package com.example.myapplication.ui.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.example.myapplication.data.model.StorageSpace

/**
 * 储物空间卡片组件 - 用于主界面的块状网格显示
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StorageSpaceCard(
    space: StorageSpace,
    onClick: () -> Unit,
    onLongClick: () -> Unit,
    modifier: Modifier = Modifier,
    itemCount: Int? = null
) {
    val haptic = LocalHapticFeedback.current
    val interactionSource = remember { MutableInteractionSource() }
    val isPressed by interactionSource.collectIsPressedAsState()
    
    // 按压缩放动画
    val scale by animateFloatAsState(
        targetValue = if (isPressed) 0.95f else 1f,
        label = "card_scale"
    )
    
    Card(
        modifier = modifier
            .fillMaxWidth()
            .graphicsLayer {
                scaleX = scale
                scaleY = scale
            }
            .combinedClickable(
                interactionSource = interactionSource,
                indication = LocalIndication.current,
                onClick = {
                    println("DEBUG: StorageSpaceCard onClick called for: ${space.name}")
                    onClick()
                },
                onLongClick = {
                    println("DEBUG: StorageSpaceCard onLongClick called for: ${space.name}")
                    haptic.performHapticFeedback(HapticFeedbackType.LongPress)
                    onLongClick()
                }
            ),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 图标 - 放在顶部，增大尺寸
            Icon(
                imageVector = getStorageSpaceIcon(space.iconType),
                contentDescription = space.name,
                modifier = Modifier.size(40.dp),
                tint = MaterialTheme.colorScheme.primary
            )

            Spacer(modifier = Modifier.height(12.dp))

            // 名称和描述 - 居中对齐
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = space.name,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.Medium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
                
                space.description?.let { description ->
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = description,
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                }
                
                // 物品数量 - 放在描述下方
                itemCount?.let { count ->
                    Spacer(modifier = Modifier.height(6.dp))
                    Text(
                        text = "${count}个",
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.primary,
                        fontWeight = FontWeight.Medium
                    )
                }
            }
        }
    }
}

/**
 * 根据图标类型返回对应的图标
 */
@Composable
private fun getStorageSpaceIcon(iconType: String?): ImageVector = when (iconType) {
    "box" -> Icons.Default.Inventory2
    "folder" -> Icons.Default.Folder
    "closet" -> Icons.Default.Checkroom
    "bedroom" -> Icons.Default.BedroomParent
    "kitchen" -> Icons.Default.Kitchen
    "garage" -> Icons.Default.Garage
    "office" -> Icons.Default.Work
    "living_room" -> Icons.Default.Living
    "electronics" -> Icons.Default.Devices
    "books" -> Icons.Default.MenuBook
    "sports" -> Icons.Default.SportsBaseball
    "tools" -> Icons.Default.Build
    else -> Icons.Default.Inventory2 // 默认使用盒子图标
} 